package configuration;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Properties;

public class TableConfiguration {
	
	/*
	 * loading databse table information.
	 * Map contains tablename and map of column and datatype
	 * Assuming Every first column is primary key.
	 * Using Linked HashMap to ensure order guarantee
	 */
	private static HashMap<String,LinkedHashMap<String,String>> tableNameAndColumnNameMap = new HashMap<String, LinkedHashMap<String,String>>();
	
	private TableConfiguration(){};
	private static TableConfiguration tc = null;
	
	public static 	LinkedHashMap<String,String> addTableConfiguration(String tableName){
		LinkedHashMap<String,String> columnNameAndTypeMap = null;
		if(tableNameAndColumnNameMap.containsKey(tableName)){
			columnNameAndTypeMap = tableNameAndColumnNameMap.get(tableName);
		}
		else{
			
			Properties prop = new Properties();
			try{
				prop.load(new FileInputStream("config.properties"));
				String columNameAndType = prop.getProperty(tableName);
				String[]columnNameAndTypeArray = columNameAndType.split(",");
				columnNameAndTypeMap = new LinkedHashMap<String, String>();
				for(int i = 0; i<columnNameAndTypeArray.length;i++){
					
					String[] array = columnNameAndTypeArray[i].split("-");
					columnNameAndTypeMap.put(array[0],array[1]);
				}
				tableNameAndColumnNameMap.put(tableName,columnNameAndTypeMap);
				
			}catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return columnNameAndTypeMap;
	}

	
	public static TableConfiguration getInstance(){
		if(tc == null){
			tc = new TableConfiguration();
		}
		return tc;
	}

}
